<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>解梦功能测试</title>
    <style>
        body {
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        textarea {
            width: 100%;
            height: 150px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            margin: 20px 0;
        }
        button {
            background: #667eea;
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            display: block;
            margin: 0 auto;
        }
        button:hover {
            background: #5a6fd8;
        }
        button:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 30px;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #667eea;
        }
        .error {
            border-left-color: #e74c3c;
            background: #fdf2f2;
            color: #e74c3c;
        }
        pre {
            white-space: pre-wrap;
            word-wrap: break-word;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>解梦功能测试</h1>
        <textarea id="dreamInput" placeholder="请输入您的梦境描述，例如：梦见蛇、梦见水、梦见飞翔..."></textarea>
        <button id="analyzeBtn">智能解梦</button>
        <div id="result"></div>
    </div>

    <script>
        document.getElementById('analyzeBtn').addEventListener('click', async function() {
            const dreamInput = document.getElementById('dreamInput');
            const resultDiv = document.getElementById('result');
            const analyzeBtn = document.getElementById('analyzeBtn');
            
            const dreamContent = dreamInput.value.trim();
            
            if (!dreamContent) {
                resultDiv.innerHTML = '<div class="result error">请输入梦境描述</div>';
                return;
            }
            
            // 禁用按钮并显示加载状态
            analyzeBtn.disabled = true;
            analyzeBtn.textContent = '分析中...';
            resultDiv.innerHTML = '';
            
            try {
                const response = await fetch('/api/dreams/interpret', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ dreamContent })
                });
                
                const data = await response.json();
                
                if (response.ok && data.success) {
                    const dream = data.data.dream;
                    resultDiv.innerHTML = `
                        <div class="result">
                            <h3>解梦结果</h3>
                            <p><strong>积极解读：</strong>${dream.interpretation}</p>
                            <p><strong>比喻：</strong>✨ ${dream.metaphor}</p>
                            <p><strong>每日鼓励：</strong>🌟 ${dream.encouragement}</p>
                            <p><strong>行动建议：</strong>💡 ${dream.action}</p>
                            <p><strong>今日运势：</strong>🔮 ${dream.prediction}</p>
                            <p><strong>心情指数：</strong>${dream.mood}/10</p>
                            <p><strong>能量指数：</strong>${dream.energy}/10</p>
                            <p><strong>自信指数：</strong>${dream.confidence}/10</p>
                            <p><strong>幸运数字：</strong>${dream.luckyNumber}</p>
                            <p><strong>幸运颜色：</strong>${dream.luckyColor}</p>
                            <p><strong>幸运方向：</strong>${dream.luckyDirection}</p>
                        </div>
                    `;
                } else {
                    resultDiv.innerHTML = `<div class="result error">解梦失败：${data.message || '未知错误'}</div>`;
                }
            } catch (error) {
                resultDiv.innerHTML = `<div class="result error">请求失败：${error.message}</div>`;
            } finally {
                // 恢复按钮状态
                analyzeBtn.disabled = false;
                analyzeBtn.textContent = '智能解梦';
            }
        });
    </script>
</body>
</html>